home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / MACVOGL- / H2V.C < prev    next >
C/C++ Source or Header  |  1992-07-19  |  4KB  |  197 lines

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include "h2v.h"
  4.  
  5. /*
  6.  *    Read the raw hersh font data and write the binary font files
  7.  *    as specified in h2v.h or as specified in an index file.
  8.  */
  9.  
  10. extern char    *malloc();
  11.  
  12. HTAB    hersh[MAX_CHARS];
  13.  
  14. void    readdata(), readindex(), writefont();
  15.  
  16. /*
  17.  * main driver - if argc > 2 we are creating a file from an
  18.  * index, otherwise use the table in h2v.h
  19.  */
  20. main(argc, argv)
  21.     int    argc;
  22.     char    **argv;
  23. {
  24.     FILE    *fp;
  25.     FTAB    table;
  26.     int    i;
  27.     
  28.     if (argc != 2 && argc != 4) {
  29.         fprintf(stderr, "Usage: h2v datafile [indexfile fontfile]\n");
  30.         exit(1);
  31.     }
  32.     if ((fp = fopen(argv[1], "r")) == NULL) {
  33.         fprintf(stderr, "h2v: can't open hersh data file %s\n", argv[1]);
  34.         exit(1);
  35.     }
  36.  
  37.     readdata(fp);
  38.  
  39.     if (argc == 4) {
  40.         readindex(argv[2], argv[3], &table);
  41.         writefont(&table);
  42.     } else
  43.         for (i = 0; i < sizeof(fonts) / sizeof(FTAB); i++)
  44.             writefont(&fonts[i]);
  45.  
  46.     exit(0);
  47. }
  48.  
  49. /*
  50.  *  readdata
  51.  *
  52.  *  Reads the raw hersh data
  53.  */
  54. void
  55. readdata(fp)
  56.     FILE    *fp;
  57. {
  58.     int    i = 0, j, x, y;
  59.     int    charno, pairs;
  60.     char    buf[MAX_BUF];
  61.     
  62.     while (getcharacter(fp, &charno, &pairs, buf)) {
  63.         hersh[charno - 1].ch = malloc(2 * pairs + 1);
  64.         strcpy(hersh[charno - 1].ch, buf);
  65.         hersh[charno - 1].len = strlen(hersh[charno - 1].ch);
  66.     }
  67.  
  68.     fclose(fp);
  69. }
  70.  
  71. /*
  72.  *  readindex
  73.  * 
  74.  *  Read an index file into index tab.
  75.  */
  76. void
  77. readindex(name, fname, tab)
  78.     char    *name, *fname;
  79.     FTAB    *tab;
  80. {
  81.     
  82.     FILE    *fp;
  83.     int    i;
  84.  
  85.     if ((fp = fopen(name, "r")) == NULL) {
  86.         fprintf(stderr, "h2v: can't open index file\n");
  87.         exit(1);
  88.     }
  89.  
  90.     tab->name = fname;
  91.  
  92.     i = 0;
  93.     while (fscanf(fp, "%d %d", &tab->ent[i], &tab->ent[i + 1]) == 2)
  94.         if ((i += 2) >= MAX_ENTS - 2) {
  95.             fprintf(stderr, "h2v: indexfile to big - increase MAX_ENTS\n");
  96.             exit(1);
  97.         }
  98.     
  99.     tab->ent[i] = 0;
  100.  
  101.     fclose(fp);
  102. }
  103.  
  104. /*
  105.  * writefont
  106.  *
  107.  *    output a font to file name based on font table tab
  108.  */
  109. void
  110. writefont(tab)
  111.     FTAB    *tab;
  112. {
  113.     int    l ,f, j, j1, x, y, k1, k2, k;
  114.     short    i, nchars, asdecw[3]; 
  115.     short    start, end, nvects, fd;
  116.     char    *p;
  117.     HTAB    *curch;
  118.  
  119.     fprintf(stderr, "Font name: %s\n", tab->name);
  120.  
  121. #ifdef PC
  122.     if ((fd = open(tab->name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644)) < 0) {
  123. #else
  124.     if ((fd = open(tab->name, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) {
  125. #endif
  126.         fprintf(stderr, "Can't open output file: %s\n", tab->name);
  127.         exit(1); 
  128.     }
  129.  
  130.     asdecw[0] = asdecw[2] = -100;
  131.     asdecw[1] = 100;
  132.     nvects = nchars = 0;
  133.  
  134.     lseek(fd, (long)(5 * sizeof(short)), 0); /*  Leave room for stuff at top */
  135.  
  136.     for (i = 0; (start = tab->ent[i]) != 0; i += 2) {
  137.         end = tab->ent[i + 1];
  138. #ifdef DEBUG
  139.         fprintf(stderr, "Char: %d to %d\n", start, end);
  140. #endif
  141.         do {
  142.             curch = &hersh[start - 1];
  143.             nchars++;
  144.             if (curch->ch == (char *)NULL) {
  145.                 fprintf(stderr, "h2v: character %d not available\n", start);
  146.                 exit(1);
  147.             }
  148.             asdecw[2] = MAX(asdecw[2], curch->ch[1] - curch->ch[0]);
  149.  
  150. #ifdef DEBUG
  151.             fprintf(stderr, "Char: %d length %d\n", start, curch->len);
  152. #endif
  153.             for (p = &curch->ch[2]; *p; p++) {
  154.                 x = *p++;
  155.                 if (x != ' ') {
  156.                     asdecw[0] = MAX(asdecw[0], COORD(*p));
  157.                     asdecw[1] = MIN(asdecw[1], COORD(*p));
  158.                 }
  159.             }
  160.  
  161.             nvects += curch->len / 2;
  162.             if (write(fd, &curch->len, sizeof(short)) != sizeof(short)) {
  163.                 fprintf(stderr,"h2v: ERROR writing character length to file\n");
  164.                 exit(1);
  165.             }
  166.             if (write(fd, curch->ch, (unsigned)curch->len) != curch->len) {
  167.                 fprintf(stderr,"h2v: ERROR writing character data to file\n");
  168.                 exit(1);
  169.             }
  170.             start++;
  171.         } while (start <= end);
  172.     }
  173.  
  174. #ifdef DEBUG
  175.     fprintf(stderr,"nchars: %d, nvects: %d\n", nchars, nvects);
  176.     fprintf(stderr,"ascender: %d, decender: %d maxwidth: %d\n",
  177.         asdecw[0], asdecw[1], asdecw[2]);
  178. #endif
  179.  
  180.     lseek(fd, 0L, 0);
  181.     if (write(fd, &nchars, sizeof(nchars)) != sizeof(nchars)) {
  182.         fprintf(stderr,"Error writing to file\n");
  183.         exit(1);
  184.     }
  185.  
  186.     if (write(fd, &nvects, sizeof(nvects)) != sizeof(nvects)) {
  187.         fprintf(stderr,"Error writing to file\n");
  188.         exit(1);
  189.     }
  190.     if (write(fd, asdecw, sizeof(asdecw)) != sizeof(asdecw)) {
  191.         fprintf(stderr, "Error writing to file\n");
  192.         exit(1);
  193.     }
  194.  
  195.     close(fd);
  196. }
  197.